package com.gui.proxy.cglib;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
import java.util.Arrays;

public class CglibProxyDemo {

    public static void main(String[] args) {

        CglibTargetService cglibTargetService = (CglibTargetService) Enhancer.create(CglibTargetService.class, new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                System.out.println("前置增强，打印参数：" + Arrays.toString(objects));
                Object result = methodProxy.invoke(new CglibTargetService(), objects);
                System.out.println("后置增强，打印返回值：" + result);
                return result;
            }
        });
        cglibTargetService.save("hello cglib...");
        System.out.println(cglibTargetService.getClass());
    }
}
